﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>用户消费</title>
    <script src="../../Scripts/jquery-1.9.1.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.fs.stepper.js" type="text/javascript"></script>
    <link href="../../CSS/NumberUpDown.css" rel="stylesheet" type="text/css" />
    <link href="../../Content/Themes/blue2/css.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .fieldCss{ border: solid 1px #C6E2FF;width: 550px;padding: 10px 10px 10px 10px;}
        .showGoodsTable{ height:300px; overflow:scroll; width:100%;}
        #showgoods{border-color:#1C1C1C;  width:400px;overflow:scroll;text-align:center; border-collapse:collapse; border-spacing:0;
        }
        #showgoods td{word-break:keep-all; white-space:nowrap; padding:3px;border:1px black solid ;}
        .showSex{ margin-left:68px;}
        #addMsg{ width:300px; height:40px;  font-size:20px; line-height:40px;}
        #account{ margin-left:110px}
        #Left{ margin-left:100px;}
        input[ type="text"]{width:150px;border:none; border-bottom:solid 1px #ccc; height:18px; line-height:18px; font-size:12px;  padding:1px 2px 1px 2px;}
    </style>
</head>
<body class="main">
    <div class="AllWebTopDiv">
        <div class="path">
            <div class="nav">
                <a href="#">用户消费</a>
            </div>
        </div>
                <div class="content">
                    <fieldset class="fieldCss">
                        <legend>用户信息</legend>
                        <form action="<%:Url.Action("UserSale","UserManager") %>" method="post">
                        <label>
                            用户编号：</label><%:Html.TextBox("userId")%>  <input  type="submit" id="selectUser" value="查询" class="button2" onclick="selectUserId();"/>
                        <label>
                            用户姓名：</label><%:Html.TextBox("userName", null, new { @readonly="readonly"})%><br />
                            
                        <br />
                        <label>
                            联系电话：</label><%:Html.TextBox("tel", null, new { @readonly = "readonly" })%>
                        <label  class="showSex">
                            用户性别：</label><%:Html.TextBox("sex", null, new { @readonly = "readonly" })%>
                            </form>
                    </fieldset>
                    <div>
                        <fieldset class="fieldCss">
                            <legend>消费信息</legend>
                            <label>
                                本次消费：</label><%:Html.TextBox("sale",0) %>
                        </fieldset>
                    </div>
                    <div>
                        <label>
                            商品：</label>
                        <select id="sgoods" >
                          
                           <% for (int i = 0; i < Model.Count; i++)
                              {%>
                              <option value="<%:Model[i].id %>" ><%:Model[i].goodsName%></option>

                           <%} %>
                        </select>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <label>
                            数量：</label>

                        <input type="number" id="num" value="0" step="1"  min="1"  readonly ="readonly"/>
                        <input  type="button" value="添加" onclick="addGoods()" class="button2" style=" margin-left:55px;"/>
                    </div>
                   
                      <table id="showgoods">
                        <tr class="DGHeaderStyle">
                        <td>商品</td>
                        <td>价格</td>
                        <td>数量</td>
                        <td>操作</td>
                        </tr>
                      </table>
                    <div>
                    <br />
                    <label>票据打印:</label>
                    <input  type="checkbox" /> &nbsp;&nbsp;&nbsp;
                    <label>附加信息：</label>
                     <input type="text"  id="addMsg"/>
                     <br /><br />
                     <input type="button" id="account" value="结算" class="button2" />
                     <input type="button" id="Left" value="退出" class="button2" onclick="closeWindows();" />
                    </div>
                </div>
    </div>
</body>
</html>
<script type="text/javascript"> 
   
   $("#num").stepper();
   //查找用户
   function selectUserId(){
          var userId=document.getElementById("userId");
           checkNaN(userId);
   }




   var  x=new Array();
   
   //添加商品
    function addGoods() {

    var goodsName,price;
    var UseMoney=parseFloat(document.getElementById("sale").value);
        var Sindex = document.getElementById("sgoods").selectedIndex;
        var Sval = document.getElementById("sgoods").options[Sindex].value;
        var num=document.getElementById("num").value;

      
        var  b= false;
         goodsName= document.getElementById("sgoods").options[Sindex].text;
              for(var i=0; i < x.length;i++)
              {
                 if(x[i]==goodsName)
                  {
                    b=true;
                    $("#showgoods tr").each(
                      function(){ 
                             var s =$(this).children("td").eq(0).text(); 
                             var s1=$(this).children("td").eq(1).text();
                             var s2=$(this).children("td").eq(2).text();
                              if(s==goodsName)
                              {
                                var shuliang=parseInt(s2); 
                                 var n=parseInt(num);
                                  shuliang += n;
                                 $(this).children("td").eq(2).text(shuliang);
                                  var Floats1=parseFloat(s1);
                                  var Floats2=parseFloat(n);
                                   UseMoney +=Floats1*Floats2;
                                   $("#sale").val(UseMoney.toFixed(2));
                              }
                            })
                  }
              }

  
         if(!b)
          {
           $.ajax({
            type:"POST",
            url:"<%:Url.Action("AddGoods","UserManager") %>",
            data:"Sval="+Sval,
            cache:false,
            success:function(txt)
               {
                var str=   txt.toString().split('|');
               
              goodsName=str[0];
              price=str[1];
           
               x.push(goodsName);
               $("#showgoods").append("<tr><td>"+goodsName+"</td><td>"+price+"</td><td>"+num+"</td><td><input type=\"button\" value=\"删除\" class=\"button2\" onclick=\"delTr(this);\"></td></tr>");
                                  var Floats1=parseFloat(price);
                                  var Floats2=parseFloat(num);
                                  UseMoney+=Floats1*Floats2;
                                    $("#sale").val(UseMoney.toFixed(2));
               }
             })
          }
        
    }

    //检查是否为数字
     function checkNaN(o) {
        if (!String.prototype.trim) {
            var TRIM_REG = /(^\s*)|(\s*$)/g;
            String.prototype.trim = function () { return this.replace(TRIM_REG, ''); }
        }
        var m = o.value.trim(); 
        
            reg = /^[0-9]*[1-9][0-9]*$/;
           
            if (!reg.test(m)) {
                alert("需要输入整数");
                return false;
            }
             return true;
    }

    //删除指定行
    function delTr(o)
    {
       
      var delPrice=parseFloat($(o).parent("td").prevAll("td").eq(1).text());
      var  delNum =parseFloat($(o).parent("td").prevAll("td").eq(0).text());
      var delgoods=$(o).parent("td").prevAll("td").eq(2).text();
      var allMoney=parseFloat($("#sale").val());
      var left=allMoney - (delPrice*delNum);
       for(var i=0 ;i<x.length;i++)
       {
          if(x[i]==delgoods)
          {
             x.splice(i,1);
          }
       }

       $("#sale").val(left.toFixed(2))
       $(o).parent("td").parent("tr").remove();
    }

    //关闭窗口
    function closeWindows()
    {
        var i = parent.layer.getFrameIndex();
        parent.layer.close(i);
    }
</script>
